<?php
namespace Task\Controller;
use Think\Controller;
class IndexController extends Controller {
    
	public function test(){
		while(true){
			echo 123;
			sleep(5);
		}
	}
	/**
	 * 同步油价
	 */
	public function sysOilPrice(){
		set_time_limit(0);
		//ignore_user_abort(true);
		ini_set('memory_limit','512M');
		
		$provinceArray=['福建省','湖北省','广西壮族自治区','上海市','广东省'];
		
		$taskData=array(
				'addTime'=>time(),
				'status'=>0
		);
			
		$taskID=D('TaskOilprice')->add($taskData);
		$update=0;
		$insert=0;
		$update_error=0;
		$insert_error=0;
		$callback='';
		
		
		foreach ($provinceArray as $key=>$pro){
			
			$type="3";
			$province = $pro;
			$city = "";
			$area = "";
			$stationsId = "";
			$stationsName = "";
			$msgArray = array(
					'type'=>$type,
					'province'=>$province,
					'city'=>$city,
					'area'=>$area,
					'stationsId'=>$stationsId,
					'stationsName'=>$stationsName
			);
			
			$signArray = array(
					$type,$province,$city,$stationsId,$stationsName
			);
			
			sort($signArray,SORT_STRING);
			$str='';
			foreach ($signArray as $a){
				$str.=$a;
			}
			//dump($str);
			$signature=sha1($str);
			
			
			$send=array(
					'methodName'=>'queryStationPrice',
					'ver'=>'1.0',
					'appNo'=>C('OIL_APPNO'),
					'appKey'=>C('OIL_KEY'),
					'sign'=>$signature,
					'msg'=>$msgArray
			);
			
			//dump($send);
			//dump((json_encode($send)));
			$url=C('OIL_URL');
			//dump($url);
			$result=http_post_json($url, json_encode($send));
			//dump($result);
			$callback.=$result;
			//file_put_contents("log/".date("Ymd")."-oil.txt", json_encode($result,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);	
			if(!$result){
				//D()->rollback();
				$taskData=array(
						'taskID'=>$taskID,
						'finishTime'=>time(),
						'status'=>2,
						'errorInfo'=>'同步油价接口异常!省份:'.$pro,
						'callback'=>$callback
				);
				
				$taskID=D('TaskOilprice')->save($taskData);
				continute;
				//exit;
			}
			//file_put_contents('sysOil', $result);
			//echo $result;
			//exit;
			$rt=json_decode($result,true);
			if ($rt['respCode']!='0000'){
				//D()->rollback();
				$taskData=array(
						'taskID'=>$taskID,
						'finishTime'=>time(),
						'status'=>2,
						'errorInfo'=>'同步油价返回失败!省份:'.$pro,
						'callback'=>$callback
				);
				
				$taskID=D('TaskOilprice')->save($taskData);
				
				//exit;
				continute;
			}
			//echo $result;
			//exit;
			
			//dump($taskID);
			//dump($result);
			//dump($rt);exit;
			$Shop = D('Shop');
			
			$Shop->startTrans();
			$final = true;
			foreach ($rt['data']['list'] as $a){
				if($a['longitude']=='')
					$a['longitude']=0;
				if($a['longitude']=='')
					$a['latitude']=0;
				$isShop=D('Shop')->where(['shopAccount'=>$a['stationsId'],'isOn'=>1])->find();
					
				//组装油价
				$oilData=array();
				$oilDataArray=array();
				!empty($a['oil0'])?$oilData['0#']=$a['oil0']:'';
				!empty($a['oil90'])?$oilData['90#']=$a['oil90']:'';
				!empty($a['oil92'])?$oilData['92#']=$a['oil92']:'';
				!empty($a['oil93'])?$oilData['93#']=$a['oil93']:'';
				!empty($a['oil95'])?$oilData['95#']=$a['oil95']:'';
				!empty($a['oil97'])?$oilData['97#']=$a['oil97']:'';
				!empty($a['oil98'])?$oilData['98#']=$a['oil98']:'';
					
				if (count($oilData)>0){
					foreach ($oilData as $key=>$oo){
						if ($oo > 0){
							$oilDataArray[]=array(
									'name'=>$key,
									//'price'=>sprintf("%.2f",substr(sprintf("%.3f", $oo), 0, -2))
									'price'=>sprintf("%.2f",$oo)
							);
						}
					}
				}
					
				if($isShop){
					//更新
					$saveData=array(
							'province'=>$a['province'],
							'city'=>$a['city'],
							'district'=>$a['area'],
							'address'=>$a['address'],
							'desc'=>$a['enterpriseName'],
							'x'=>$a['longitude'],
							'y'=>$a['latitude'],
							'shopName'=>$a['stationsName'],
							'oilPriceJson'=>json_encode($oilDataArray),
							'updateOilPriceTime'=>time()
					);
			
					$save=D('Shop')->where(['shopID'=>$isShop['shopID']])->save($saveData);
					if($save){
						$update++;
					}
					else{
						$errorIDs[]=$a['stationsId'];
						$update_error++;
						$final = false;
					}
			
				}
				else{
					//新增
					$saveData=array(
							'shopAccount'=>$a['stationsId'],
							'province'=>$a['province'],
							'city'=>$a['city'],
							'district'=>$a['area'],
							'address'=>$a['address'],
							'desc'=>$a['enterpriseName'],
							'x'=>$a['longitude'],
							'y'=>$a['latitude'],
							'shopName'=>$a['stationsName'],
							'oilPriceJson'=>json_encode($oilDataArray),
							'addTime'=>time(),
							'shopType'=>1,
							'updateOilPriceTime'=>time(),
							'headImg'=>'common_shop.jpg'
					);
			
					$save=D('Shop')->add($saveData);
					if($save){
						$insert++;
					}
					else{
						$errorIDs[]=$a['stationsId'];
						$insert_error++;
						$final = false;
					}
				}
					
			}
			if($final){
				$Shop->commit();
			}else{
				$Shop->rollback();
				$taskData=array(
						'taskID'=>$taskID,
						'finishTime'=>time(),
						'status'=>2,
						'errorInfo'=>'同步油价返回失败!省份:'.$pro,
						'callback'=>$callback
				);
				$taskID=D('TaskOilprice')->save($taskData);
				continute;
			}
			
		}
		
		
		
		$taskData=array(
			'update'=>$update,
			'insert'=>$insert,
			'insertError'=>$insert_error,
			'updateError'=>$update_error,
			'updateErrorIDs'=>implode(',', $errorIDs),
			'finishTime'=>time(),
			'status'=>1,
			'taskID'=>$taskID
		);
		$updateTask=D('TaskOilprice')->save($taskData);
		echo "同步完成！";
		exit;
	}
    
	public function test(){
		$data='{"test":6.33}';
		dump(json_decode($data));
	}
    
}